package com.translate.web.api.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;

import com.common.util.ListConvertMap;
import com.common.util.MergeDataSource;
import com.response.vo.ResponseModel;
import com.translate.entity.Manuscript;
import com.translate.entity.OrderLog;
import com.translate.vo.OrderLogVO;
import com.translate.web.config.contract.IOSSConfig;
import com.translate.web.constant.RedisConstant;
import com.translate.web.param.IdAndUserIdParam;
import com.translate.web.service.IManuscriptService;
import com.translate.web.service.IOrderLogService;

@RestController
@RequestMapping("orderLog")
public class OrderLogController {
	private IOrderLogService service;

	IOSSConfig config;
	
	private IManuscriptService manuscriptService;
	
	@Autowired
    public void setConfig(IOSSConfig config) {
        this.config = config;
    }

	@Autowired
	public void setManuscriptService(IManuscriptService manuscriptService) {
		this.manuscriptService = manuscriptService;
	}

	public OrderLogController(IOrderLogService service) {
		this.service = service;
	}

	/**
	 * 订单日志列表
	 * @param param
	 * @param nativeWebRequest
	 * @return
	 */
	@PostMapping("listOrderLogs")
	public ResponseModel<List<OrderLogVO>> listOrderLogs(@RequestBody @Validated IdAndUserIdParam param, NativeWebRequest nativeWebRequest) {
		
		long userId = (long) nativeWebRequest.getAttribute("userId", 0);
		
		param.setUserId((int) userId);
		
		String orderLogKey = RedisConstant.getOrderLogKey(param.getId(), userId);
		

		
		List<OrderLogVO> datasByIdFromCache = this.service.getDatasByIdFromCache(param, OrderLogVO.class, orderLogKey, 3);
		
		 // 查找 稿件表
        int[] ids = datasByIdFromCache.stream().mapToInt(e->e.getId()).distinct().toArray();
		
        if (0 == ids.length) {
        	return ResponseModel.success(datasByIdFromCache);
        }
        
        List<Manuscript> dataByIds = manuscriptService.getDataByIds(ids);
        
        Map<Integer, List<Manuscript>> listToMapList = ListConvertMap.listToMapList(dataByIds, Manuscript::getOrderLogId);
        
        MergeDataSource<OrderLogVO> mergeDataSource = new MergeDataSource<OrderLogVO>(datasByIdFromCache);
        
        String url = config.getURL();
        
        mergeDataSource.mergeRelease(OrderLogVO::getId, listToMapList, (OrderLogVO t, List<Manuscript> v) -> {
        	
        		for (Manuscript manuscript : v) {
					
        			manuscript.setFile(url.concat(manuscript.getFile()));
				}
        	
				t.setManuscript(v);
				return t;
		});
        
		return ResponseModel.success(datasByIdFromCache);
	}
}